package love.yuanmengrobot.als.system.dao.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/6/13 16:32
 */

import love.yuanmengrobot.als.system.dao.AnnounccementDao;
import love.yuanmengrobot.als.system.javaBean.Alsannouncement;
import love.yuanmengrobot.als.utilPublic.JdbcUtil;
import love.yuanmengrobot.als.utilPublic.SqlString;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class AnnounccementDaoImpl implements AnnounccementDao {

    @Override
    public boolean deleteAnnouncement(String id) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.deleteAnnouncementSql(id));
            if (s == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }
        return false;
    }

    @Override
    public boolean addAnnouncement(Alsannouncement alsannouncement) {


        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            stmt = conn.createStatement();
            int s = stmt.executeUpdate(SqlString.insertAnnouncementSql(alsannouncement.getPublisher(), alsannouncement.getTitile(), alsannouncement.getNotice_content()));
            if (s == 1) {
                return true;
            } else {
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {//回收资源
            JdbcUtil.close(rs, stmt, conn);
        }


        return false;
    }

    @Override
    public boolean updateAnnouncement(Alsannouncement alsannouncement) {

        if (alsannouncement.getNoticeID() != null && alsannouncement.getNoticeID().length() > 0) {
            Connection conn = null;
            Statement stmt = null;
            ResultSet rs = null;
            try {
                conn = JdbcUtil.getConnection();
                stmt = conn.createStatement();
                int s = stmt.executeUpdate(SqlString.updateAnnouncementSql(alsannouncement.getNoticeID(), alsannouncement.getTitile(), alsannouncement.getNotice_content()));
                if (s == 1) {
                    return true;
                } else {
                    return false;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {//回收资源
                JdbcUtil.close(rs, stmt, conn);
            }
        }
        return false;
    }
}
